package com.example.openguass.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.openguass.entity.CommonResult;
import com.example.openguass.entity.Customer;
import com.example.openguass.entity.DishFlavor;
import com.example.openguass.mapper.DishFlavorMapper;
import com.example.openguass.service.DishFlavorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.openguass.service.DishService;
import com.example.openguass.validation.Update;
import com.example.openguass.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@Service
public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorMapper, DishFlavor> implements DishFlavorService {
    private DishFlavorMapper dishFlavorMapper;
    private DishService dishService;
    @Autowired
    private void setDishFlavorMapper(DishFlavorMapper dishFlavorMapper){
        this.dishFlavorMapper = dishFlavorMapper;
    }
    @Autowired
    private void setDishService(DishService dishService){
        this.dishService = dishService;
    }

    @Override
    public Boolean exit(DishFlavorVO dishFlavorVO) {
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id",dishFlavorVO.getDishId());
        wrapper.eq("name",dishFlavorVO.getName());
        return dishFlavorMapper.selectCount(wrapper) > 0;
    }

    @Override
    public Boolean insert(DishFlavorVO dishFlavorVO) {
        DishFlavor dishFlavor = new DishFlavor();
        BeanUtils.copyProperties(dishFlavorVO,dishFlavor);
        dishFlavor.setCreateDate(LocalDateTime.now());
        return dishFlavorMapper.insert(dishFlavor) > 0;
    }

    @Override
    public Boolean update(DishFlavorVO dishFlavorVO) {
        DishFlavor dishFlavor = new DishFlavor();
        BeanUtils.copyProperties(dishFlavorVO,dishFlavor);
        dishFlavor.setUpdateDate(LocalDateTime.now());
        return dishFlavorMapper.updateById(dishFlavor) > 0;
    }

    @Override
    public IPage<DishFlavorReturnVO> findPage(DishFlavorPageVO pageVO) {
        Page<DishFlavor> page = new Page<>(pageVO.getPageNo(),pageVO.getPageSize());
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_date");
        IPage<DishFlavor> dishFlavorIPage = dishFlavorMapper.selectPage(page,wrapper);
        IPage<DishFlavorReturnVO> dishFlavorReturnVOIPage = new Page<>(pageVO.getPageNo(),pageVO.getPageSize());
        List<DishFlavor> dishFlavorList = dishFlavorIPage.getRecords();
        List<DishFlavorReturnVO> dishFlavorReturnVOList = new ArrayList<>();
        for (DishFlavor dishFlavor : dishFlavorList){
            DishFlavorReturnVO dishFlavorReturnVO = new DishFlavorReturnVO();
            BeanUtils.copyProperties(dishFlavor,dishFlavorReturnVO);
            if (dishService.getById(dishFlavor.getDishId()) != null){
                dishFlavorReturnVO.setDishName(dishService.getById(dishFlavor.getDishId()).getName());
            }
            dishFlavorReturnVOList.add(dishFlavorReturnVO);
        }
        dishFlavorReturnVOIPage.setRecords(dishFlavorReturnVOList);
        return dishFlavorReturnVOIPage;
    }

    @Override
    public DishFlavor getDishFlavorByDIshId(String dishId) {
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id",dishId);
        return dishFlavorMapper.selectOne(wrapper);
    }
}
